package com.atguigu.controller;


import com.atguigu.entity.CartInfo;
import com.atguigu.result.RetVal;
import com.atguigu.service.CartInfoService;
import com.atguigu.util.AuthContextHolder;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 购物车表 用户登录系统时更新冗余 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/cart")
public class CartInfoController {
    @Autowired
    private CartInfoService cartInfoService;
    @Autowired
    private RedisTemplate redisTemplate;
    //1.加入购物车
    @GetMapping("addCart/{skuId}/{skuNum}")
    public RetVal addCart(@PathVariable Long skuId, @PathVariable Integer skuNum, HttpServletRequest request){
        String oneOfUserId="";
        //判断id 为临时id 还是 登录id
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)){
            oneOfUserId=AuthContextHolder.getUserTempId(request);
        }else {
            oneOfUserId=userId;
        }
        cartInfoService.addToCart(oneOfUserId,skuId,skuNum);
        return RetVal.ok();
    }

    //2.购物车列表
     @GetMapping("getCartList")
    public RetVal getCartList(HttpServletRequest request){
        List<CartInfo> cartInfoList= cartInfoService.getCartList(request);
        return RetVal.ok(cartInfoList);
     }
     //3.购物车勾选 http://api.gmall.com/cart/checkCart/24/1
     @GetMapping("checkCart/{skuId}/{isChecked}")
     public RetVal checkCart(@PathVariable Long skuId, @PathVariable Integer isChecked, HttpServletRequest request){
         String oneOfUserId="";
         //判断id 为临时id 还是 登录id
         String userId = AuthContextHolder.getUserId(request);
         if(StringUtils.isEmpty(userId)){
             oneOfUserId=AuthContextHolder.getUserTempId(request);
         }else {
             oneOfUserId=userId;
         }
         cartInfoService.checkCart(skuId,isChecked,oneOfUserId);
         return RetVal.ok();
     }
     //4.购物车删除 http://api.gmall.com/cart/deleteCart/24
    @DeleteMapping("deleteCart/{skuId}")
    public RetVal  deleteCart(@PathVariable Long skuId,HttpServletRequest request){
        String oneOfUserId="";
        //判断id 为临时id 还是 登录id
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)){
            oneOfUserId=AuthContextHolder.getUserTempId(request);
        }else {
            oneOfUserId=userId;
        }
        cartInfoService.deleteCart(oneOfUserId,skuId);
        return RetVal.ok();
    }

    //5.查询勾选的购物车清单---送货清单，其他微服务调用
    @GetMapping("getSelectedCartInfo/{userId}")
    public List<CartInfo> getSelectedCartInfo(@PathVariable String userId){
        List<CartInfo> cartInfoListIsCheck= cartInfoService.getSelectedCartInfo(userId);
        return cartInfoListIsCheck;
    }

}

